from threading import Thread,Lock

#  需求：采用两个线程，分别对g_num累加100万次
# 创建一把互斥锁
lock = Lock()
g_num = 0


def sum_num1():
    lock.acquire() # 获得锁
    for i in range(10000000):
        global g_num
        g_num += 1
    print(f'线程1累加之后的结果{g_num}')
    lock.release() # 释放锁


def sum_num2():
    lock.acquire()
    for i in range(10000000):
        global g_num
        g_num += 1
    print(f'线程2累加之后的结果{g_num}')
    lock.release()


if __name__ == '__main__':
    t1 = Thread(target=sum_num1)
    t2 = Thread(target=sum_num2)

    t1.start()
    t2.start()
